home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / GMS / Source / Asm / Demos / Stars4.s < prev    next >
Encoding:
Text File  |  1997-05-02  |  7.2 KB  |  264 lines

  1. ;-------T-------T------------------------T----------------------------------;
  2. ;3D STARFIELD
  3. ;------------
  4. ;This is a demo of a triple buffered starfield.  I didn't orginally write
  5. ;this starfield code, but it was very old so it needed a fair bit of
  6. ;cleaning up to work with the library.  It now runs in 4 colours for a
  7. ;little more depth too...
  8.  
  9.     INCDIR    "INCLUDES:"
  10.     INCLUDE    "games/games_lib.i"
  11.     INCLUDE    "games/games.i
  12.  
  13. CALL    MACRO
  14.     jsr    _LVO\1(a6)
  15.     ENDM
  16.  
  17. NSTARS    =    800                     ;Number of stars
  18.  
  19. XSPEED    =    -4
  20. YSPEED    =    6
  21. ZSPEED    =    2
  22.  
  23. SCRWIDTH =    320
  24. SCRHEIGHT =    256
  25.  
  26.     SECTION    "Stars",CODE
  27.  
  28. ;==========================================================================;
  29. ;                             INITIALISE DEMO
  30. ;==========================================================================;
  31.  
  32.     STARTGMS
  33.  
  34. Start:    MOVEM.L    A0-A6/D1-D7,-(SP)
  35.     move.l    GMSBase(pc),a6
  36.     lea    ScreenTags(pc),a0
  37.     CALL    ShowScreen
  38.     tst.l    d0
  39.     beq.s    .Error_Screen
  40.  
  41.     bsr.s    Main
  42.  
  43. .ReturnToDOS
  44.     move.l    GMSBase(pc),a6
  45.     move.l    Screen(pc),a0
  46.     CALL    DeleteScreen
  47. .Error_Screen
  48.     MOVEM.L    (SP)+,A0-A6/D1-D7
  49.     moveq    #ERR_OK,d0
  50.     rts
  51.  
  52. ;==========================================================================;
  53. ;                                 INITIALISE
  54. ;==========================================================================;
  55.  
  56.     ;Randomize star coordinates
  57.  
  58. Main:    lea    StarCoords,a0            ;a0 = Ptr to star co-ordinates.
  59.     move.w    #NSTARS-1,d7
  60. .loop1    move.w    #8192,d1
  61.     CALL    SlowRandom
  62.     move.w    d0,(a0)+
  63.     CALL    SlowRandom
  64.     move.w    d0,(a0)+
  65.     CALL    SlowRandom
  66.     move.w    d0,(a0)+
  67.     dbra    d7,.loop1
  68.  
  69.     ;Construct perspective table
  70.  
  71.     lea    PersTable,a0             ;a0 = ptr to perspective table.
  72.     moveq    #0,d1                    ;d1 = Starting at 0.
  73. .loop2    move.l    #$95FFFF,d2              ;d2 = $95FFFF
  74.     move.l    d1,d3                    ;d3 = d1
  75.     add.w    #300,d3                  ;d3 = ++300
  76.     divu    d3,d2                    ;d2 = ($95ffff)/d3
  77.     move.w    d2,(a0)+                 ;a0 = d2+
  78.     addq.w    #1,d1                    ;d1 = ++1
  79.     cmp.w    #8192,d1                 ;d1 = Equal to 8192?
  80.     bne.s    .loop2
  81.  
  82.     ;Construct plot tables for fast drawing.
  83.  
  84.     lea    PlotXTable,a0            ;a0 = X table - byte positions.
  85.     lea    PlotBTable,a1            ;a1 = Bit table (X related).
  86.     lea    PlotYTable,a2            ;a2 = Y table - line position.
  87.     moveq    #0,d0                    ;d0 = 00
  88. .loop3    move.w    d0,d1                    ;d1 = d0
  89.     lsr.w    #3,d1                    ;d1 = (d0)<<3
  90.     move.w    d1,(a0)+                 ;a0 = d1+
  91.  
  92.     move.w    d0,d1                    ;d1 = d0
  93.     eor.w    #$FFFF,d1                ;d1 = (d0) eor $ffff
  94.     and.w    #%00000111,d1            ;d1 = &%00000111
  95.     move.w    d1,(a1)+                 ;a1 = BitSet++
  96.  
  97.     cmp.w    #SCRHEIGHT,d0    ;Write out the Y values for the
  98.     bge.s    .plot2    ;table.
  99.     move.w    d0,d1    ;d1 = Line Number.
  100.     mulu    #80,d1    ;d1 = (LineNumber)*80
  101.     move.w    d1,(a2)+    ;a2 = (LineNumber*80)++
  102.  
  103. .plot2    addq.w    #1,d0
  104.     cmp.w    #SCRWIDTH,d0
  105.     bne.s    .loop3
  106.  
  107. ;==========================================================================;
  108. ;                                MAIN LOOP
  109. ;==========================================================================;
  110.  
  111. MainLoop:
  112.     move.l    GMSBase(pc),a6
  113.     move.l    Screen(pc),a0
  114.     CALL    WaitVBL
  115.     CALL    SwapBuffers
  116.  
  117. ;==========================================================================;
  118. ;                             STAR ANIMATION
  119. ;==========================================================================;
  120.  
  121.     movem.w    StarXPos(pc),d0/d1/d2    ;MV = d0/d1/d2 = XPos/YPos/ZPos
  122.     add.w    #XSPEED,d0               ;d0 = (StarXPos)+XSPEED
  123.     add.w    #YSPEED,d1               ;d1 = (StarYPos)+YSPEED
  124.     add.w    #ZSPEED,d2               ;d2 = (StarZPos)+ZSPEED
  125.     and.w    #%0000011111111111,d0
  126.     and.w    #%0000011111111111,d1
  127.     and.w    #%0000011111111111,d2
  128.     movem.w    d0/d1/d2,StarXPos
  129.  
  130.     lea    Sinus(pc),a0             ;a0 = Sinus table.
  131.     movem.w    StarXAdd(pc),d3/d4/d5    ;MV = d3/d4/d5 : X/Y/Z
  132.     add.w    (a0,d0.w),d3
  133.     add.w    (a0,d1.w),d4
  134.     add.w    (a0,d2.w),d5
  135.     movem.w    d3/d4/d5,StarXAdd
  136.  
  137. ;===========================================================================;
  138. ;                              SCREEN CLEAR
  139. ;===========================================================================;
  140.  
  141.     move.l    GMSBase(pc),a6
  142.     move.l    Screen(pc),a0
  143.     moveq    #BUFFER3,d0
  144.     CALL    ClrScreen
  145.  
  146. ;==========================================================================;
  147. ;                               DRAW STARS
  148. ;==========================================================================;
  149.  
  150.     lea    StarCoords,a0            ;Draw starfield
  151.     lea    PersTable,a1
  152.     lea    PlotXTable,a2
  153.     lea    PlotBTable,a3
  154.     lea    PlotYTable,a4
  155.     move.l    Screen(pc),a6
  156.     move.l    GS_MemPtr2(a6),a6
  157.  
  158.     movem.w    StarXAdd(pc),d3/d4/d5    ;MV = d3/d4/d5 : ?
  159.     add.w    #4096,d3                 ;d3 = ++4096
  160.     add.w    #4096,d4                 ;d4 = ++4096
  161.  
  162.     move.w    #NSTARS-1,d7
  163.  
  164. .draw1    movem.w    (a0)+,d0/d1/d2           ;MV = d0/d1/d2 : XPos/YPos/ZPos.
  165.     add.w    d3,d0                    ;Increase XPos.
  166.     and.w    #8191,d0                 ;d0 = And'd
  167.     sub.w    #4096,d0                 ;d0 = --4096
  168.  
  169.     add.w    d4,d1                    ;Y-movement
  170.     and.w    #8191,d1                 ;d1 = And'd
  171.     sub.w    #4096,d1                 ;d1 = --4096
  172.  
  173.     add.w    d5,d2                    ;Z-movement
  174.     and.w    #8191,d2
  175.     add.w    d2,d2                    ;d2 = *2 [word]
  176.     move.w    (a1,d2.w),d6             ;d6 = Read from Perspective table.
  177.  
  178.     muls    d6,d0                    ;X-projection
  179.     swap    d0
  180.     add.w    #176,d0
  181.  
  182.     cmp.w    #SCRWIDTH-1,d0
  183.     bhi.s    .nodraw
  184.     muls    d6,d1                    ;Y-projection
  185.     swap    d1
  186.     add.w    #136,d1
  187.     cmp.w    #SCRHEIGHT-1,d1
  188.     bhi.s    .nodraw
  189.  
  190.     add.w    d0,d0                    ;d0 = *2 [word]
  191.     add.w    d1,d1                    ;d1 = *2 [word]
  192.     move.w    (a4,d1.w),d6             ;d6 = Plot Y.
  193.     add.w    (a2,d0.w),d6             ;d6 = ++PlotX.
  194.     move.w    (a3,d0.w),d0             ;d0 = BitValue.
  195.  
  196.     cmp.w    #7000,d2                 ;Now draw the star according to
  197.     bgt.s    .draw2                   ;its position in the Z axis.
  198.     bset    d0,(a6,d6.w)
  199.     dbra    d7,.draw1
  200.     bra.s    .done
  201.  
  202. .draw2    cmp.w    #13000,d2
  203.     bgt.s    .draw3
  204.     bset    d0,SCRWIDTH/8(a6,d6.w)
  205.     dbra    d7,.draw1
  206.     bra.s    .done
  207.  
  208. .draw3    bset    d0,(a6,d6.w)
  209.     bset    d0,SCRWIDTH/8(a6,d6.w)
  210. .nodraw    dbra    d7,.draw1
  211.  
  212. .done    move.l    GMSBase(pc),a6
  213.     moveq    #JPORT1,d0    ;Read from port 1 (mouse).
  214.     moveq    #JT_ZBXY,d1
  215.     CALL    ReadJoyPort    ;Go get joystick status.
  216.     btst    #MB_LMB,d0
  217.     beq    MainLoop
  218.     rts
  219.  
  220. ;===========================================================================;
  221. ;                                  DATA
  222. ;===========================================================================;
  223.  
  224. ScreenTags:
  225.     dc.l    TAGS_GAMESCREEN
  226. Screen:    dc.l    0
  227.     dc.l    GSA_Palette,.palette
  228.     dc.l    GSA_ScrWidth,SCRWIDTH
  229.     dc.l    GSA_ScrHeight,SCRHEIGHT
  230.     dc.l    GSA_Planes,2
  231.     dc.l    GSA_Attrib,TPLBUFFER
  232.     dc.l    GSA_ScrType,ILBM
  233.     dc.l    TAGEND
  234.  
  235. .palette
  236.     dc.l    $000000,$D0D0D0,$606060,$202020
  237.  
  238. ;===========================================================================;
  239. ;                                STAR DATA
  240. ;===========================================================================;
  241.  
  242. StarXAdd dc.w    33                       ;Star stuff
  243. StarYAdd dc.w    12
  244. StarZAdd dc.w    -114
  245.  
  246. StarXPos dc.w    0                        ;Sinus positions
  247. StarYPos dc.w    310
  248. StarZPos dc.w    1280
  249.  
  250.     INCLUDE    "GMS:source/asm/demos/StarSinus.i"
  251.  
  252.     SECTION    Storage,BSS
  253.  
  254. StarCoords
  255.     ds.w    NSTARS*3                 ;Star coordinates
  256.  
  257. PersTable
  258.     ds.w    8192                     ;Perspective table
  259.  
  260. PlotXTable ds.w    SCRWIDTH                ;Plot tables
  261. PlotBTable ds.w    SCRWIDTH
  262. PlotYTable ds.w    SCRHEIGHT
  263.  
  264.